package com.jeesite.modules.sys.wx.qy.service;

import com.jeesite.common.config.Global;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.modules.ajz.entity.AjzRecord;
import com.jeesite.modules.ajz.service.AjzCompanyInfoService;
import com.jeesite.modules.ajz.service.AjzRecordService;
import com.jeesite.modules.sys.wx.qy.util.MNews;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 定时器
 */
@Service
@Lazy(false)
public class Timertask {
    @Autowired
    private AjzRecordService ajzRecordService;
    @Autowired
    private AjzCompanyInfoService ajzCompanyInfoService;


//    @Scheduled(cron = "0 */1 * * * ?") //每一分钟执行一次
    @Scheduled(cron = "0 0 7 * * ?")   //每天七点执行一次
    public void TaskWithDate()throws Exception{

        System.out.println("--------启动定时器------");
        String agentid = Global.getConfig("ajz.qy.agenId");
        //1.过滤掉status不为0，messagePush状态为0和整改日期为空的店面,
        List<AjzRecord> recordlist = ajzRecordService.findAll();
        //2.1今天整点时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date nowDate = sdf.parse(DateUtils.getDate());
        System.out.println("今天整点时间:"+nowDate);
        System.out.println("今天整点时间戳:"+nowDate.getTime());
        //2.2明天整天时间
        Date tomorrowDate = DateUtils.addDays(nowDate, 1);
        System.out.println("明天整点时间:"+tomorrowDate);
        System.out.println("明天整点时间戳:"+tomorrowDate.getTime());

        for(AjzRecord record:recordlist){
            //2.3将整改时间转为Date获取时间戳
            Date deadlineDate = record.getDeadline();
            Long deadline = deadlineDate.getTime();
            System.out.println("整改整点时间:"+deadlineDate);
            System.out.println("整改整点时间戳:"+deadline);
            //3判断当前日期的前一天是否等于整改日期
            if(tomorrowDate.getTime()==deadline){
                //向对应的企业端人员推送消息
                senMessage(record,nowDate,deadlineDate,agentid);
            }
        }
    }


    /**
     * 发送消息
     * @return
     */
    public void senMessage(AjzRecord record, Date nowDate,Date deadlineDate, String agentid) throws Exception{
        //获取更新者
        String touser=record.getUpdateBy();

        //获取企业名称
        String baseId =record.getBaseId();
        String companyName = ajzCompanyInfoService.getById(baseId).getEnterpriseName();

        //URL http://admin.cenfortune.com/a/ajz/ajzBaseinfo/searchName?name=NAME&id=ID
        StringBuffer sb_url = new StringBuffer();
        sb_url.append(Global.getConfig("sys.url"));
        sb_url.append("/a/ajz/ajzBaseinfo/searchName?name=");
        sb_url.append(companyName);
        sb_url.append("&id=");
        sb_url.append(baseId);
        sb_url.append("&user=");
        sb_url.append(touser);
        String url = URLDecoder.decode(sb_url.toString(), "utf-8");
        System.out.println("-------------链接地址------------;"+url);

        //项目模板填值
        String now = DateUtils.formatDate(nowDate,"yyyy-MM-dd");
        String deadline = DateUtils.formatDate(deadlineDate, "yyyy-MM-dd");
        String remind = "请相关工作人员做好后续复查工作";
        StringBuffer content = new StringBuffer();
        content.append(companyName);
        content.append("存在的隐患问题将于");
        content.append(deadline);
        content.append("到期");

        //向企业微信推送消息
        MNews.sendMessages(touser,"@all","@all",agentid ,"到期消息提醒",now,content.toString(),remind, url);

    }



}
